package application;

import org.htmlparser.beans.StringBean;

import fetch.paginegialle.Fetcher;

import utils.config.Config;

public class MainFetcher {
	
	/*
	 * THESE LINKS WILL NOT BE AVAILABLE FROM JUNE 2010
	 */
	
	private String OUT_DIR;
	private int START;
	private int END;
	private String URL;
	private String KMLPATH;
	
	public static void main(String[] args) throws Exception {
		MainFetcher mf = new MainFetcher();
		mf.fetch();
		
		System.out.println("Done");
	}
	
	public MainFetcher() {
		
		Config config = new Config(this.getClass().getSimpleName()+".ser"); 
		
		OUT_DIR = (String)config.get("OUT_DIR");
		START = (Integer)config.get("START");
		END = (Integer)config.get("END");
		URL = (String)config.get("URL");
		KMLPATH = (String)config.get("KMLPATH");
		
	}
	
	public void fetch() throws Exception {
		
		//construct the url
		for(int i = START; i<= END; i++){
			StringBean sb = new StringBean();
		    sb.setLinks (true);
		    sb.setURL(URL+i);
		    String page = sb.getStrings();
		    
		    //find kml key
		    String kmlKey = parse(page);
		    
		    //save kml
			
		    if(kmlKey != null){
		    	Fetcher f = new Fetcher(); 
		    	String kml_file = OUT_DIR+"/"+i+".kml";
		    	String query = KMLPATH+kmlKey;
		    	
                f.fetch(query,kml_file);
				
				Thread.sleep(1000);
		    }
			
		}

		
	}
	
	public static String parse(String page){
		String url = null;
		
		int indexPath = page.indexOf("kml?file=");
		
		
		if(indexPath > 0)
			url = page.substring(indexPath+9,indexPath+45);
		
		return url;
	}
	

}
